import akshare as ak
import numpy as np
import pandas as pd
from orm.t_gp_sssj import TGpSssj

def refresh_sssj():
    np.set_printoptions(threshold=np.inf)

    # 加了这一行那表格的一行就不会分段出现了
    pd.set_option('display.width', 1000)
    # 显示所有列
    pd.set_option('display.max_columns', None)
    # 显示所有行
    pd.set_option('display.max_rows', None)

    stock_zh_a_spot_df = ak.stock_zh_a_spot()
    print(stock_zh_a_spot_df)

    for row in stock_zh_a_spot_df.itertuples():
        print(getattr(row, 'code'))
        code = getattr(row, 'code')
        if code == '300771':
            gp = TGpSssj.select().where(TGpSssj.code == code)
            if gp:
                TGpSssj.update({
                    TGpSssj.symbol: getattr(row, 'symbol'),
                    TGpSssj.name: getattr(row, 'name'),
                    TGpSssj.trade: getattr(row, 'trade'),
                    TGpSssj.pricechange: getattr(row, 'pricechange'),
                    TGpSssj.changepercent: getattr(row, 'changepercent'),
                    TGpSssj.buy: getattr(row, 'buy'),
                    TGpSssj.sell: getattr(row, 'sell'),
                    TGpSssj.settlement: getattr(row, 'settlement'),
                    TGpSssj.open: getattr(row, 'open'),
                    TGpSssj.high: getattr(row, 'high'),
                    TGpSssj.low: getattr(row, 'low'),
                    TGpSssj.volume: getattr(row, 'volume'),
                    TGpSssj.amount: getattr(row, 'amount'),
                    TGpSssj.ticktime: getattr(row, 'ticktime'),
                    TGpSssj.per: getattr(row, 'per'),
                    TGpSssj.pb: getattr(row, 'pb'),
                    TGpSssj.mktcap: getattr(row, 'mktcap'),
                    TGpSssj.nmc: getattr(row, 'nmc'),
                    TGpSssj.turnoverratio: getattr(row, 'turnoverratio'),
                }).where(TGpSssj.code == code).execute()
            else:
                new_gp = TGpSssj(
                    symbol=getattr(row, 'symbol'),
                    code=code,
                    name=getattr(row, 'name'),
                    trade=getattr(row, 'trade'),
                    pricechange=getattr(row, 'pricechange'),
                    changepercent=getattr(row, 'changepercent'),
                    buy=getattr(row, 'buy'),
                    sell=getattr(row, 'sell'),
                    settlement=getattr(row, 'settlement'),
                    open=getattr(row, 'open'),
                    high=getattr(row, 'high'),
                    low=getattr(row, 'low'),
                    volume=getattr(row, 'volume'),
                    amount=getattr(row, 'amount'),
                    ticktime=getattr(row, 'ticktime'),
                    per=getattr(row, 'per'),
                    pb=getattr(row, 'pb'),
                    mktcap=getattr(row, 'mktcap'),
                    nmc=getattr(row, 'nmc'),
                    turnoverratio=getattr(row, 'turnoverratio'))
                new_gp.save()

if __name__=="__main__":
    refresh_sssj()
